home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / printing / rlpr-1.000 / rlpr-1 / rlpr-1.13 / rlprd.c < prev    next >
C/C++ Source or Header  |  1996-06-30  |  7KB  |  224 lines

  1. /* filename: rlprd.c
  2.  * project: rlpr
  3.  * author: meem  --  meem@sherilyn.wustl.edu
  4.  * version: $Id: rlprd.c,v 1.6 1996/06/01 02:45:28 meem Exp $
  5.  * contents: daemon which does "reflecting" of messages from nonprivileged
  6.  *           to priviledged ports
  7.  *
  8.  * Time-stamp: <1996/05/31 21:44 -- meem@sherilyn.wustl.edu>
  9.  */
  10.  
  11. /* copyright (c) 1996 meem, meem@gnu.ai.mit.edu
  12.  *
  13.  * This program is free software; you can redistribute it and/or modify
  14.  * it under the terms of the GNU General Public License as published by
  15.  * the Free Software Foundation; either version 1, or (at your option)
  16.  * any later version.
  17.  *
  18.  * This program is distributed in the hope that it will be useful,
  19.  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  20.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21.  * General Public License for more details.
  22.  */
  23.  
  24. #include <sys/types.h>          /* for pid_t */
  25. #include <signal.h>
  26. #include <sys/wait.h>
  27. #include <netdb.h>              /* got gethostbyname() */
  28. #include <sys/time.h>           /* select() and macros */
  29. #include <sys/stat.h>           /* for umask() */
  30. #include <unistd.h>             /* for setsid() */
  31. #include <string.h>             /* for strerror() */
  32. #include <syslog.h>             /* logging facilities */
  33. #include <errno.h>
  34. #include <stdio.h>              /* BUFSIZ */
  35. #include <sys/socket.h>         /* for bind(), connect(), etc */
  36. #include <netinet/in.h>         /* for sockaddr_in */
  37. #include <sys/wait.h>
  38. #include <stdarg.h>
  39. #include "rlpr-common.h"
  40.  
  41.  
  42. /* TO DO: more robust signal handling...!
  43.  */
  44.  
  45. /* function prototypes */
  46.  
  47. void   daemon_init(void);
  48. void   rlprd_fatal(char *function, ...);
  49. void   reapchild(int unused);
  50. const char * inc_host(struct sockaddr_in *sin);
  51.  
  52. int main(int argc, char *argv[]) {
  53.   struct hostent *hp;
  54.   struct sockaddr_in sin_in;    /* where we will listen */
  55.   struct sockaddr_in sin_out;   /* our projected identity (to lpd) */
  56.   struct sockaddr_in sin_lpd;   /* the connection we will send to lpd */  
  57.   struct sockaddr_in sin_from;  /* the connection that came in */
  58.   int inc_rlprd, sd_in, out_lpd;/* socket descriptors */
  59.   int sin_fromlen;        /* the length of the incoming descriptor */
  60.   int nc, nfds;                 /* data moved in read/write */
  61.   char buf[BUFSIZ];             /* for moving data */
  62.   char printhost[MAXHOSTNAMELEN]; /* name of printhost */
  63.   pid_t pid;                    /* for fork() */
  64.   fd_set readfds;               /* for select() */
  65.   int i;            /* dummy variable */
  66.   int c;
  67.   
  68.   name = *argv;
  69.  
  70.   daemon_init();
  71.   get_local_hostname();         /* sets global variable */
  72.          
  73.   if (!(hp = gethostbyname(local_hostname)))
  74.     rlprd_fatal("gethostbyname on %s", local_hostname);
  75.   
  76.   if ((sd_in = socket(AF_INET, SOCK_STREAM, 0)) < 0) /* 0 == auto proto config */
  77.     rlprd_fatal("socket");
  78.   
  79.   /* zero out structs to get the unused part zeroed */
  80.   memset(&sin_in,  0, sizeof(sin_in));
  81.   memset(&sin_out, 0, sizeof(sin_out));
  82.   memset(&sin_lpd, 0, sizeof(sin_lpd));
  83.   
  84.   sin_in.sin_family = sin_out.sin_family = sin_lpd.sin_family = AF_INET;
  85.   memcpy(&sin_in.sin_addr,  hp->h_addr, hp->h_length);
  86.   memcpy(&sin_out.sin_addr, hp->h_addr, hp->h_length); 
  87.   sin_in.sin_port  = htons(RLPRD_TO_NUM);
  88.   sin_lpd.sin_port = htons(LPD_TO_NUM);
  89.   
  90.   if (bind(sd_in,  (struct sockaddr *) &sin_in,  sizeof(sin_in)) < 0) {
  91.     if (errno == EADDRINUSE)
  92.       syslog(LOG_WARNING, "port %i in use: is %s already running?", RLPRD_TO_NUM, name);
  93.     else
  94.       syslog(LOG_WARNING, "bind to port %i failed: %m", RLPRD_TO_NUM);
  95.     exit(1);
  96.   }
  97.   
  98.   listen(sd_in, 5);             /* don't rely on more than 5 */
  99.   signal(SIGCHLD, reapchild);
  100.  
  101.   while (1) {
  102.     if ((inc_rlprd = accept(sd_in, (struct sockaddr *) &sin_from, &sin_fromlen)) < 0)
  103.       if (errno != EINTR) syslog(LOG_WARNING, "accept: %m");
  104.       else continue;
  105.  
  106.     if ((pid = fork()) < 0) rlprd_fatal("fork");
  107.     else if (!pid) {
  108.       /* CHILD */
  109.  
  110.       signal(SIGCHLD, SIG_IGN);
  111.   
  112.       i = 0;
  113.       do
  114.     if (read(inc_rlprd, &printhost[i], 1) < 0)
  115.       rlprd_fatal("unable to read proxy hostname");
  116.       while (printhost[i++] != '\n');
  117.       printhost[--i] = '\0';
  118.       
  119.       syslog(LOG_INFO,"proxy from %s to %s", inc_host(&sin_from), printhost);
  120.  
  121.       /* bind our local socket so we come from a privileged port */
  122.  
  123.       if ((out_lpd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  124.     rlprd_fatal("socket");
  125.  
  126.       for (i = LO_LPD_FROM_NUM; i <= HI_LPD_FROM_NUM; i++) {
  127.     sin_out.sin_port = htons(i);
  128.     if (bind(out_lpd, (struct sockaddr *) &sin_out, sizeof(sin_out)) >= 0)
  129.       break;
  130.       }
  131.  
  132.       if (i > HI_LPD_FROM_NUM) {    /* didn't get a port */
  133.     syslog(LOG_WARNING, "bind to ports %i-%i: %m",
  134.            LO_LPD_FROM_NUM, HI_LPD_FROM_NUM);
  135.     exit(1);
  136.       }
  137.  
  138.       /* fill in remaining part of sin_lpd struct with printhost */
  139.  
  140.       if (!(hp = gethostbyname(printhost)))
  141.     rlprd_fatal("gethostbyname on printhost");
  142.       memcpy(&sin_lpd.sin_addr, hp->h_addr, hp->h_length); 
  143.  
  144.       if (connect(out_lpd, (struct sockaddr *) &sin_lpd, sizeof(sin_lpd)) < 0)
  145.         rlprd_fatal("connect");
  146.       
  147.       while (1) {                               /* conversation loop */
  148.  
  149.     FD_ZERO(&readfds);                      /* initialize fds structure */
  150.         FD_SET(inc_rlprd, &readfds);
  151.         FD_SET(out_lpd, &readfds);
  152.     
  153.         if ((nfds = select(16, &readfds, NULL, NULL, NULL)) < 0) /* is 16 right here!? */
  154.       rlprd_fatal("select");
  155.     
  156.         if (nfds) {
  157.           if (FD_ISSET(inc_rlprd, &readfds)) {         /* data from rlpr client */
  158.             if ((nc = read(inc_rlprd, buf, sizeof(buf))) < 0)
  159.               rlprd_fatal("read1");
  160.             else if (!nc) break;
  161.             else write(out_lpd, buf, nc);
  162.           }
  163.           if (FD_ISSET(out_lpd, &readfds)) {     /* data from lpd server */
  164.             if ((nc = read(out_lpd, buf, sizeof(buf))) < 0)
  165.               rlprd_fatal("read2");
  166.             else if (!nc) break;
  167.             else write(inc_rlprd, buf, nc);
  168.           }
  169.         }
  170.       }
  171.       exit(0);
  172.     }
  173.     /* PARENT */
  174.     close(inc_rlprd);
  175.   }
  176.   exit(0);
  177. }
  178.  
  179. void daemon_init(void) {
  180.   pid_t pid;
  181.  
  182.   /* make sure euid == root */
  183.   if (geteuid() != 0) {
  184.     fprintf(stderr, "%s must be run root or setuid root!\n", name);
  185.     exit(1);
  186.   }
  187.  
  188.   openlog("rlprd", LOG_PID, LOG_LPR);
  189.  
  190.   if ((pid = fork()) < 0) rlprd_fatal("fork");
  191.   else if (pid) exit(0);        /* kill off parent */
  192.   /* CHILD */
  193.   
  194.   setsid();                     /* now unique session, PGID, no ctty */
  195.   chdir("/");                   /* in case we were on a mounted partition */
  196.   umask(0);                     /* clear file mode creation mask */
  197. }
  198.  
  199. const char * inc_host(struct sockaddr_in *sin) {
  200.   struct hostent *hp;
  201.   hp = gethostbyaddr((char *) &sin->sin_addr,
  202.              sizeof(struct in_addr), sin->sin_family);
  203.   if (!hp) rlprd_fatal("gethostbyaddr");
  204.   return hp->h_name;
  205. }
  206.      
  207. void reapchild(int unused) {
  208.   int olderrno = errno;
  209.   while (waitpid( -1, NULL, WNOHANG) > 0) /* NULL BODY */;
  210.   signal(SIGCHLD, reapchild);
  211.   errno = olderrno;
  212. }
  213.  
  214. void rlprd_fatal(char *fmt, ...) {
  215.   char buf[BUFSIZ];
  216.   va_list ap;
  217.   va_start(ap, fmt);
  218.   vsprintf(buf, fmt, ap);
  219.   strcat(buf, ": %m");
  220.   syslog(LOG_ERR, buf);
  221.   va_end(ap);
  222.   exit(1);
  223. }
  224.